package cn.schoolwow.util.test.module.compare.instanceList.flow;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.util.domain.compare.instanceList.CompareInstanceList;
import cn.schoolwow.util.domain.compare.instanceList.CompareInstanceListResponse;
import cn.schoolwow.util.domain.compare.instanceList.UpdateInstanceListCompare;
import cn.schoolwow.util.test.module.compare.instanceList.domain.AddProjectTimeNode;
import org.junit.Assert;

import java.util.List;

public class CompareInstanceListQueryProcessFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        setCompareInstanceListResponse(flowContext);
        checkAddCompareList(flowContext);
        checkUpdateCompareList(flowContext);
        checkDeleteInstanceList(flowContext);
    }

    @Override
    public String name() {
        return "比较实例列表";
    }

    private void setCompareInstanceListResponse(FlowContext flowContext){
        List<AddProjectTimeNode> beforeInstanceList = (List<AddProjectTimeNode>) flowContext.checkData("beforeInstanceList");
        List<AddProjectTimeNode> afterInstanceList = (List<AddProjectTimeNode>) flowContext.checkData("afterInstanceList");

        CompareInstanceListResponse<AddProjectTimeNode> compareInstanceListResponse = CompareInstanceList.newInstance()
                .beforeInstanceList(beforeInstanceList)
                .afterInstanceList(afterInstanceList)
                .addCheckFieldNames(new String[]{"name"})
                .updateCheckFieldNames(new String[]{"name", "deadline"})
                .execute();
        flowContext.putCurrentFlowData("compareInstanceListResponse", compareInstanceListResponse);
    }

    private void checkAddCompareList(FlowContext flowContext){
        CompareInstanceListResponse<AddProjectTimeNode> compareInstanceListResponse = (CompareInstanceListResponse<AddProjectTimeNode>) flowContext.checkData("compareInstanceListResponse");
        List<AddProjectTimeNode> expectAddCompareList = (List<AddProjectTimeNode>) flowContext.checkData("addCompareList");

        List<AddProjectTimeNode> addCompareList = compareInstanceListResponse.getAddCompareList();
        Assert.assertEquals(expectAddCompareList, addCompareList);
    }

    private void checkUpdateCompareList(FlowContext flowContext){
        CompareInstanceListResponse<AddProjectTimeNode> compareInstanceListResponse = (CompareInstanceListResponse<AddProjectTimeNode>) flowContext.checkData("compareInstanceListResponse");
        List<UpdateInstanceListCompare<AddProjectTimeNode>> expectUpdateInstanceListCompareList = (List<UpdateInstanceListCompare<AddProjectTimeNode>>) flowContext.checkData("updateInstanceListCompareList");

        List<UpdateInstanceListCompare<AddProjectTimeNode>> updateInstanceListCompareList = compareInstanceListResponse.getUpdateInstanceListCompareList();
        Assert.assertEquals(expectUpdateInstanceListCompareList.size(), updateInstanceListCompareList.size());
        for(int i=0;i<updateInstanceListCompareList.size();i++){
            UpdateInstanceListCompare<AddProjectTimeNode> updateInstanceListCompare = updateInstanceListCompareList.get(i);
            UpdateInstanceListCompare<AddProjectTimeNode> expectUpdateInstanceListCompare = expectUpdateInstanceListCompareList.get(i);

            Assert.assertEquals(expectUpdateInstanceListCompare.beforeInstance, updateInstanceListCompare.beforeInstance);
            Assert.assertEquals(expectUpdateInstanceListCompare.afterInstance, updateInstanceListCompare.afterInstance);
        }
    }

    private void checkDeleteInstanceList(FlowContext flowContext){
        CompareInstanceListResponse<AddProjectTimeNode> compareInstanceListResponse = (CompareInstanceListResponse<AddProjectTimeNode>) flowContext.checkData("compareInstanceListResponse");
        List<AddProjectTimeNode> expectDeleteCompareList = (List<AddProjectTimeNode>) flowContext.checkData("deleteCompareList");

        List<AddProjectTimeNode> deleteCompareList = compareInstanceListResponse.getDeleteCompareList();
        Assert.assertEquals(expectDeleteCompareList, deleteCompareList);
    }
}
